package org.dashboard.connector;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class DERBYConnectionFactory {
	static String remotedb = "localhost";
	static String portdb = "1527";
	static String namedb = "DERBYDATASKY";
	static String driver = "org.apache.derby.jdbc.ClientDriver";
	static String connectionURL = "jdbc:derby://" + remotedb + ":" + portdb + "/" + namedb
	+ ";create=true;user=app;password=app";
	
	public static Connection getConnection() throws SQLException, ClassNotFoundException{
		Class.forName(driver);
		return DriverManager.getConnection(connectionURL);
	}
	

	public static void closeConnection(Connection conn, Statement stmt, ResultSet rs) throws Exception{
		close(conn, stmt, rs);
	}

	public static void close(Connection conn, Statement stmt, ResultSet rs) throws Exception{
		try{
			if(rs != null) rs.close();
			if(stmt != null) stmt.close();
			if(conn!=null) conn.close();
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
	}
	
	public static void closeJDBCConnection(final Connection conn) throws SQLException 
	{
		if (conn != null)
		{
			 try
			 {
				conn.close();
			 }
			 catch (SQLException ex)
			 {
				 throw new SQLException(ex.getMessage());
			 }
		}
	}

	public static void closeStatement(final Statement stmt) throws SQLException
	{
		if (stmt != null)
		{
			 try
			 {
				stmt.close();
			 }
			 catch (SQLException ex)
			 {
				 throw new SQLException(ex.getMessage());
			 }
		}
	}

	public static void closeResultSet(final ResultSet rs) throws SQLException
	{
		if (rs != null)
		{
			 try
			 {
				rs.close();
			 }
			 catch (SQLException ex)
			 {
				 throw new SQLException(ex.getMessage());
			 }
		}
	}


	
}
